$(function() {
    //alert(11)
    $('.icon-arrow-left').on('click', function() {
        location.href = './address.html'
    })

    let sex = 0
    let label = ''
    $('.spanChecked i').on('click', function() {
        $(this).addClass('iActive').parent().siblings('.spanChecked').children('i').removeClass('iActive')
        let num = $('i.iActive').parent().text().trim()
            //console.log(num);
        if (num === '先生') {
            sex = 1
        } else if (num === '女士') {
            sex = 0
        }
        //console.log(sex);
    })
    $('.hlq_laber span').on('click', function() {
        $(this).addClass('spanActiveSchool').siblings().removeClass('spanActiveSchool')
        label = $('span.spanActiveSchool').html().trim()
            //console.log(label);
    })



    $('.divSave').on('click', function() {
        const consignee = $('.hlq_name').val().trim()
        const detail = $('.hlq_address').val().trim()
        const phone = $('.hlq_phone').val().trim()
            //正则表达式
        let reg1 = /^1[3|4|5|6|7|8|9]\d{9}$/
        if (consignee === '' || detail === '' || phone === '') {
            return layui.layer.msg('请填写完整', { icon: 5 })
        }
        if (!reg1.test(phone)) {
            return layui.layer.msg('请输入正确的手机格式', { icon: 5, time: 1500 })
        }
        console.log(reg1.test(phone));
        $.ajax({
                method: 'post',
                url: 'http://121.4.102.44:8083/addressBook',
                data: JSON.stringify({
                    consignee,
                    detail,
                    label,
                    phone,
                    sex
                }),
                contentType: "application/json",
                success(res) {
                    console.log(res);
                    if (res.code !== 1) {
                        return layui.layer.msg('添加失败', { icon: 5 })
                    }
                    layui.layer.msg('添加成功', { icon: 6, time: 500 }, function() {
                        location.href = './address.html'
                    })


                }
            })
            //console.log(consignee, detail, phone, sex, label);
    })

})